import get from '@dfeidao/atom-web/local/get';
import remove from '@dfeidao/atom-web/local/remove';
import set from '@dfeidao/atom-web/local/set';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import render from '@dfeidao/atom-web/render/render';
// import whoami from '@feidao/web/atom/browser/msg/whoami';
import s_get from '@dfeidao/atom-web/storage/get';
import { IFeidaoAiBrowserComponent } from '@dfeidao/web/interfaces';

import P00 from './P00';
import P01 from './P01';
import P02 from './P02';
import P04 from './P04';
import P05 from './P05';
import P06 from './P06';


interface Users {
	page_size: number;
	page_no: number;
	dataArray: string[];
	menu_no: string;
	productid: string;
}




export default async function a007(fd: IFeidaoAiBrowserComponent) {
	if (get(fd, 'curr_dept')) {
		remove(fd, 'curr_dept');
	}
	// const user = await whoami(get_user_info);

	const productid = await s_get('productid');

	const asd = fd.data.node.querySelector<HTMLSelectElement>('#product_clent_type');
	const client_point_id = asd.value;
	const client_style = asd.selectedOptions[0].innerText;
	set(fd, 'client_style', client_style);
	set(fd, 'client_point_id', client_point_id);
	set(fd, 'productid', productid);
	const res1 = await nodejs<Users>('menu/zj-000001/s006', {
		productid, page_no: 1, page_size: get(fd, 'page_size'), client_type: client_point_id
	});
	if (res1.dataArray.length !== 0) {
		set(fd, 'flag', 1);
		set(fd, 'dataFlag', 1);
		render(fd, res1.dataArray, P06, 'P06', 'inner');
		// console.log(res1.dataArray, '000');

		res1.page_no = 1;
		res1.page_size = get(fd, 'page_size');
		render(fd, res1, P05, 'P05', 'inner');
		fd.data.node.querySelector('[data-feidao-presentation="P04"]').innerHTML = '';
	} else {
		set(fd, 'flag', 0);
		render(fd, '', P06, 'P06', 'inner');
		fd.data.node.querySelector('[data-feidao-presentation="P05"]').innerHTML = '';
		render(fd, '', P04, 'P04', 'inner');
	}
	// debugger;

	const res2 = await nodejs<{ data: string, cdata: object[], length: number }>('menu/zj-000001/s007', { productid, client_type: client_point_id });
	if (res2.length === 0) {
		render(fd, '', P02, 'P02', 'inner');
		fd.data.node.querySelector('[data-feidao-presentation="P01"]').innerHTML = '';

		render(fd, '', P00, 'P00', 'inner');
	} else {
		render(fd, JSON.stringify(res2), P01, 'P01', 'inner');
		fd.data.node.querySelector('[data-feidao-presentation="P00"]').innerHTML = '';
		render(fd, JSON.stringify(res2), P02, 'P02', 'inner');
	}
}
